﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HU8.Helpers.Base;
using XMK.Tadong.Biz;

namespace XMK.Tadong.Web.Handlers
{
    public partial class getTargetNodes : System.Web.UI.Page
    {
        int num = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.Form["id"];
            if (id == null)
                id = "0";
            TB_Tree_ObjectBiz biz = new TB_Tree_ObjectBiz();
            
                DataTable dtBig = biz.GetSubTreeTarget(id);
                DataTable dtSmall = new DataTable();
                dtSmall.Columns.Add(dtBig.Columns["obj_id"].ColumnName = "id");
                dtSmall.Columns.Add(dtBig.Columns["obj_name"].ColumnName = "name");
                DataColumn dc = new DataColumn();
                dc.ColumnName = "state";
                dc.DataType = typeof(string);
                dc.DefaultValue = "closed";
                dtSmall.Columns.Add(dc);
                dtSmall.Clear();

                for (int i = 0; i < dtBig.Rows.Count; i++)
                {
                    DataRow row = dtSmall.NewRow();
                    row["id"] = dtBig.Rows[i]["id"];
                    row["name"] = dtBig.Rows[i]["name"];
                    if (string.IsNullOrEmpty(dtBig.Rows[i]["SH_SQL"].ToString()) && dtBig.Rows[i]["P_OBJ_ID"].ToString()!="0")
                    {
                        row["state"] = "open";
                    }
                    dtSmall.Rows.Add(row);
                }

            DataTable dt = new DataTable();
            dt = biz.GetNodeById(id);
            if (dt.Rows.Count == 1 && (!string.IsNullOrEmpty(dt.Rows[0]["SH_SQL"].ToString())))
            {
                dt = biz.QuerySql(dt.Rows[0]["SH_SQL"].ToString());

                dt.Columns[0].ColumnName = "name";

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow row = dtSmall.NewRow();
                    row["id"] = DateTime.Now.Ticks + num; num++;
                    row["name"] = dt.Rows[i]["name"];
                    row["state"] = "open";
                    dtSmall.Rows.Add(row);
                }
                
            }
            if (dtSmall.Rows.Count == 0)
            {
                DataRow row1 = dtSmall.NewRow();
                row1["id"] = DateTime.Now.Ticks + num; num++;
                row1["name"] = "not data";
                row1["state"] = "open";
                dtSmall.Rows.Add(row1);
            }
            Response.Write(JsonHelper.ToJson(dtSmall));
            Response.End();
            return;
            //else
            //{
            //    DataTable dtBig = biz.GetSubTreeTarget(id);
            //    DataTable dtSamll = biz.GetSubTreeTarget(id);
            //    dtSamll.Columns.Add(dtBig.Columns["obj_id"].ColumnName = "id");
            //    dtSamll.Columns.Add(dtBig.Columns["obj_name"].ColumnName = "name");
            //    DataColumn dc = new DataColumn();
            //    dc.ColumnName = "state";
            //    dc.DataType = typeof(string);
            //    dc.DefaultValue = "closed";
            //    dtSamll.Columns.Add(dc);
            //    dtSamll.Clear();

            //    for (int i = 0; i < dtBig.Rows.Count; i++)
            //    {
            //        DataRow row = dtSamll.NewRow();
            //        row["id"] = dtBig.Rows[i]["id"];
            //        row["name"] = dtBig.Rows[i]["name"];
            //        row["state"] = "closed";
            //        dtSamll.Rows.Add(row);
            //    }

            //    Response.Write(JsonHelper.ToJson(dtSamll));
            //    Response.End();
            //    return;
            //}


        }
    }
}